package com.huawei.hms.framework.network.grs.b;

import android.content.Context;
import android.os.SystemClock;
import android.text.TextUtils;
import com.huawei.hms.framework.common.ExceptionCode;
import com.huawei.hms.framework.common.Logger;
import com.huawei.hms.framework.common.NetworkUtil;
import com.huawei.hms.framework.common.StringUtils;
import com.huawei.hms.framework.common.hianalytics.HianalyticsHelper;
import com.huawei.hms.framework.network.grs.GrsBaseInfo;
import com.huawei.hms.framework.network.grs.b.b.a;
import com.huawei.hms.framework.network.grs.b.b.c;
import com.huawei.hms.framework.network.restclient.hianalytics.HianalyticsData;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.json.JSONArray;
import org.json.JSONObject;

public class b implements a {
    private static final String a = b.class.getSimpleName();
    private GrsBaseInfo b;
    private c c;
    private ArrayList<Future<c>> d = new ArrayList();
    private ArrayList<c> e = new ArrayList();
    private JSONArray f = new JSONArray();
    private ArrayList<String> g = new ArrayList();
    private c h;
    private long i = 1;

    public b(GrsBaseInfo grsBaseInfo) {
        this.b = grsBaseInfo;
        c();
    }

    private void a(c cVar, a aVar) {
        try {
            JSONObject jSONObject = new JSONObject();
            if (cVar.h() == 0) {
                Exception f = cVar.f();
                if (f != null) {
                    jSONObject.put(HianalyticsData.ERROR_CODE, ExceptionCode.getErrorCodeFromException(f));
                    jSONObject.put(HianalyticsData.EXCEPTION_NAME, f.getClass().getSimpleName());
                    jSONObject.put(HianalyticsData.EXCEPTION_MESSAGE, StringUtils.anonymizeMessage(f.getMessage()));
                    jSONObject.put(HianalyticsData.DOMAIN, new URL(cVar.g()).getHost());
                    jSONObject.put(HianalyticsData.REQ_TOTAL_TIME, cVar.i());
                }
            } else if (cVar.e()) {
                aVar.put(HianalyticsData.ERROR_CODE, (long) cVar.h());
                aVar.put(HianalyticsData.EXCEPTION_NAME, (long) cVar.d());
                aVar.put(HianalyticsData.DOMAIN, new URL(cVar.g()).getHost());
                aVar.put(HianalyticsData.REQ_TOTAL_TIME, cVar.i());
            } else {
                jSONObject.put(HianalyticsData.ERROR_CODE, cVar.h());
                jSONObject.put(HianalyticsData.EXCEPTION_NAME, cVar.d());
                jSONObject.put(HianalyticsData.DOMAIN, new URL(cVar.g()).getHost());
                jSONObject.put(HianalyticsData.REQ_TOTAL_TIME, cVar.i());
            }
            if (jSONObject.length() != 0) {
                this.f.put(jSONObject);
            }
        } catch (Throwable e) {
            Logger.w(a, "createJsonArray catch JSONException", e);
        } catch (Throwable e2) {
            Logger.w(a, "report host MalformedURLException", e2);
        }
    }

    private void a(ArrayList<c> arrayList, long j) {
        final Context a = com.huawei.hms.framework.network.grs.c.b.a();
        if (a != null && arrayList != null && arrayList.size() > 0 && HianalyticsHelper.getInstance().isEnableReportNoSeed(a)) {
            final long j2 = j;
            final ArrayList<c> arrayList2 = arrayList;
            HianalyticsHelper.getInstance().getReportExecutor().submit(new Runnable(this) {
                final /* synthetic */ b d;

                public void run() {
                    a aVar = new a();
                    aVar.put(HianalyticsData.TOTAL_TIME, j2);
                    aVar.put(HianalyticsData.NETWORK_TYPE, (long) NetworkUtil.getNetworkType(a));
                    int size = arrayList2.size();
                    c cVar = (c) arrayList2.get(size - 1);
                    if (cVar != null) {
                        aVar.put(HianalyticsData.REQ_TOTAL_TIME, cVar.i());
                        aVar.put(HianalyticsData.ERROR_CODE, (long) cVar.h());
                        aVar.put(HianalyticsData.EXCEPTION_NAME, (long) cVar.d());
                        try {
                            aVar.put(HianalyticsData.DOMAIN, new URL(cVar.g()).getHost());
                        } catch (Throwable e) {
                            Logger.w(b.a, "report host MalformedURLException", e);
                        }
                    }
                    for (int i = 0; i < size - 1; i++) {
                        if (arrayList2.get(i) != null) {
                            this.d.a((c) arrayList2.get(i), aVar);
                        }
                    }
                    if (this.d.f.length() != 0) {
                        aVar.put(HianalyticsData.FAILED_INFO, this.d.f.toString());
                    }
                    HianalyticsHelper.getInstance().onEvent(aVar.get(), "networkkit_grs");
                }
            });
        }
    }

    private c b(c cVar) {
        int size = this.d.size();
        int i = 0;
        c cVar2 = cVar;
        while (i < size && (cVar2 == null || !cVar2.e())) {
            c cVar3;
            try {
                cVar3 = (c) ((Future) this.d.get(i)).get(40000, TimeUnit.MILLISECONDS);
            } catch (Throwable e) {
                Logger.w(a, "when check result, find CancellationException, check others", e);
                cVar3 = cVar2;
            } catch (Throwable e2) {
                Logger.w(a, "when check result, find ExecutionException, check others", e2);
                cVar3 = cVar2;
            } catch (Throwable e22) {
                Logger.w(a, "when check result, find InterruptedException, check others", e22);
                cVar3 = cVar2;
            } catch (TimeoutException e3) {
                Logger.w(a, "when check result, find TimeoutException, cancel current request task");
                if (!((Future) this.d.get(i)).isCancelled()) {
                    ((Future) this.d.get(i)).cancel(true);
                }
                cVar3 = cVar2;
            }
            i++;
            cVar2 = cVar3;
        }
        return cVar2;
    }

    private c b(ExecutorService executorService) {
        c cVar;
        Object obj;
        Throwable th;
        Throwable th2;
        int i;
        long elapsedRealtime = SystemClock.elapsedRealtime();
        int size = this.g.size();
        c cVar2 = null;
        int i2 = 0;
        while (i2 < size) {
            String str = (String) this.g.get(i2);
            if (!TextUtils.isEmpty(str)) {
                Future submit = executorService.submit(new d(str, i2, this));
                this.d.add(submit);
                try {
                    cVar = (c) submit.get(this.i, TimeUnit.SECONDS);
                    if (cVar != null) {
                        try {
                            if (cVar.e()) {
                                obj = 1;
                                if (obj == null) {
                                    Logger.v(a, "needBreak is true so need break current circulation");
                                    break;
                                }
                                i2++;
                                cVar2 = cVar;
                            }
                        } catch (Throwable e) {
                            th = e;
                            cVar2 = cVar;
                            th2 = th;
                            Logger.w(a, "the computation was cancelled", th2);
                            cVar = cVar2;
                            i = 1;
                            if (obj == null) {
                                i2++;
                                cVar2 = cVar;
                            } else {
                                Logger.v(a, "needBreak is true so need break current circulation");
                                cVar = b(cVar);
                                a(this.e, SystemClock.elapsedRealtime() - elapsedRealtime);
                                return cVar;
                            }
                        } catch (Throwable e2) {
                            th = e2;
                            cVar2 = cVar;
                            th2 = th;
                            Logger.w(a, "the computation threw an ExecutionException", th2);
                            cVar = cVar2;
                            obj = null;
                            if (obj == null) {
                                Logger.v(a, "needBreak is true so need break current circulation");
                                cVar = b(cVar);
                                a(this.e, SystemClock.elapsedRealtime() - elapsedRealtime);
                                return cVar;
                            }
                            i2++;
                            cVar2 = cVar;
                        } catch (Throwable e22) {
                            th = e22;
                            cVar2 = cVar;
                            th2 = th;
                            Logger.w(a, "the current thread was interrupted while waiting", th2);
                            cVar = cVar2;
                            i = 1;
                            if (obj == null) {
                                i2++;
                                cVar2 = cVar;
                            } else {
                                Logger.v(a, "needBreak is true so need break current circulation");
                                cVar = b(cVar);
                                a(this.e, SystemClock.elapsedRealtime() - elapsedRealtime);
                                return cVar;
                            }
                        } catch (TimeoutException e3) {
                            cVar2 = cVar;
                            Logger.w(a, "the wait timed out");
                            cVar = cVar2;
                            obj = null;
                            if (obj == null) {
                                Logger.v(a, "needBreak is true so need break current circulation");
                                cVar = b(cVar);
                                a(this.e, SystemClock.elapsedRealtime() - elapsedRealtime);
                                return cVar;
                            }
                            i2++;
                            cVar2 = cVar;
                        }
                    }
                    obj = null;
                } catch (CancellationException e4) {
                    th2 = e4;
                    Logger.w(a, "the computation was cancelled", th2);
                    cVar = cVar2;
                    i = 1;
                    if (obj == null) {
                        i2++;
                        cVar2 = cVar;
                    } else {
                        Logger.v(a, "needBreak is true so need break current circulation");
                        cVar = b(cVar);
                        a(this.e, SystemClock.elapsedRealtime() - elapsedRealtime);
                        return cVar;
                    }
                } catch (ExecutionException e5) {
                    th2 = e5;
                    Logger.w(a, "the computation threw an ExecutionException", th2);
                    cVar = cVar2;
                    obj = null;
                    if (obj == null) {
                        Logger.v(a, "needBreak is true so need break current circulation");
                        cVar = b(cVar);
                        a(this.e, SystemClock.elapsedRealtime() - elapsedRealtime);
                        return cVar;
                    }
                    i2++;
                    cVar2 = cVar;
                } catch (InterruptedException e6) {
                    th2 = e6;
                    Logger.w(a, "the current thread was interrupted while waiting", th2);
                    cVar = cVar2;
                    i = 1;
                    if (obj == null) {
                        i2++;
                        cVar2 = cVar;
                    } else {
                        Logger.v(a, "needBreak is true so need break current circulation");
                        cVar = b(cVar);
                        a(this.e, SystemClock.elapsedRealtime() - elapsedRealtime);
                        return cVar;
                    }
                } catch (TimeoutException e7) {
                    Logger.w(a, "the wait timed out");
                    cVar = cVar2;
                    obj = null;
                    if (obj == null) {
                        Logger.v(a, "needBreak is true so need break current circulation");
                        cVar = b(cVar);
                        a(this.e, SystemClock.elapsedRealtime() - elapsedRealtime);
                        return cVar;
                    }
                    i2++;
                    cVar2 = cVar;
                }
                if (obj == null) {
                    Logger.v(a, "needBreak is true so need break current circulation");
                    break;
                }
                i2++;
                cVar2 = cVar;
            }
            cVar = cVar2;
            obj = null;
            if (obj == null) {
                Logger.v(a, "needBreak is true so need break current circulation");
                break;
            }
            i2++;
            cVar2 = cVar;
        }
        cVar = cVar2;
        cVar = b(cVar);
        a(this.e, SystemClock.elapsedRealtime() - elapsedRealtime);
        return cVar;
    }

    private void c() {
        c a = com.huawei.hms.framework.network.grs.b.a.a.a();
        if (a == null) {
            Logger.w(a, "g*s***_se****er_conf*** maybe has a big error");
            return;
        }
        a(a);
        List<String> a2 = a.a();
        if (a2 == null || a2.size() <= 0) {
            Logger.v(a, "maybe grs_base_url config with [],please check.");
        } else if (a2.size() > 10) {
            throw new IllegalArgumentException("grs_base_url's count is larger than MAX value 10");
        } else {
            String b = a.b();
            if (a2.size() > 0) {
                for (String str : a2) {
                    if (str.startsWith("https://")) {
                        StringBuilder stringBuilder = new StringBuilder();
                        stringBuilder.append(str);
                        stringBuilder.append(String.format(Locale.ROOT, b, new Object[]{this.b.getAppName()}));
                        Object grsReqParamJoint = this.b.getGrsReqParamJoint(false, false);
                        if (!TextUtils.isEmpty(grsReqParamJoint)) {
                            stringBuilder.append("?");
                            stringBuilder.append(grsReqParamJoint);
                        }
                        this.g.add(stringBuilder.toString());
                    } else {
                        Logger.w(a, "grs server just support https scheme url,please check.");
                    }
                }
            }
        }
    }

    public c a() {
        return this.h;
    }

    public c a(final ExecutorService executorService) {
        if (this.g == null) {
            return null;
        }
        c cVar;
        try {
            c a = a();
            Logger.v(a, "getSyncServicesUrls grsQueryTimeout{%d}s", Integer.valueOf(a != null ? a.c() : 10));
            cVar = (c) executorService.submit(new Callable<c>(this) {
                final /* synthetic */ b b;

                public c a() {
                    return this.b.b(executorService);
                }

                public /* synthetic */ Object call() {
                    return a();
                }
            }).get((long) r0, TimeUnit.SECONDS);
        } catch (Throwable e) {
            Logger.w(a, "getSyncServicesUrls the computation was cancelled", e);
            cVar = null;
        } catch (Throwable e2) {
            Logger.w(a, "getSyncServicesUrls the computation threw an ExecutionException", e2);
            cVar = null;
        } catch (Throwable e22) {
            Logger.w(a, "getSyncServicesUrls the current thread was interrupted while waiting", e22);
            cVar = null;
        } catch (TimeoutException e3) {
            Logger.w(a, "getSyncServicesUrls the wait timed out");
            cVar = null;
        } catch (Throwable e222) {
            Logger.w(a, "getSyncServicesUrls catch Exception", e222);
            cVar = null;
        }
        return cVar;
    }

    public void a(c cVar) {
        this.h = cVar;
    }

    public synchronized void a(c cVar) {
        this.e.add(cVar);
        if (this.c != null && this.c.e()) {
            Logger.v(a, "grsResponseResult is ok");
        } else if (cVar.e()) {
            this.c = cVar;
            com.huawei.hms.framework.network.grs.a.a.a(this.b, this.c);
            int i = 0;
            while (i < this.d.size()) {
                if (!(((String) this.g.get(i)).equals(cVar.g()) || ((Future) this.d.get(i)).isCancelled())) {
                    Logger.v(a, "future cancel");
                    ((Future) this.d.get(i)).cancel(true);
                }
                i++;
            }
        } else {
            Logger.v(a, "grsResponseResult has exception so need return");
        }
    }
}
